<template>
  <!-- 会员异常管理 -->
  <div class="container">
    <el-row v-if="isCanBack" class="go-back">
      <el-col>
        <el-button class="fr" size="mini" type="success" @click="$router.go(-1)">返回上一页</el-button>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="20">
        <el-input v-model="query.memberId" placeholder="请输入会员ID" size="small" class="width-180" clearable />
        <el-input v-model="query.nickName" placeholder="请输入会员昵称" size="small" class="width-180" clearable />
        <el-input v-model="query.tel" placeholder="请输入手机号码" size="small" class="width-180" clearable />
        <el-input v-model="query.ruleNameFragment" placeholder="请输入触犯规则关键字" size="small" class="width-180" clearable />
        <el-date-picker
          v-model="searchDate"
          :picker-options="pickerOptions"
          type="daterange"
          range-separator="~"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          size="small"
          @change="dateChange"/>
        <el-button type="primary" icon="el-icon-search" size="small" @click="search">搜索</el-button>
      </el-col>
    </el-row>
    <el-menu :default-active="query.type" mode="horizontal" @select="changeType" >
      <el-menu-item v-for="(item,key) in userTypeDic" :key="key" :index="key">{{ item }}</el-menu-item>
    </el-menu>
    <!-- 列表 -->
    <el-table
      v-loading="listLoading"
      ref="table"
      :data="tableData"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      style="width: 100%;margin-top:15px;"
     >
     
      <el-table-column label="编号" width="50" align="center">
        <template slot-scope="scope">{{ scope.$index+1 }}</template>
      </el-table-column>
      <el-table-column label="判断时间" align="center" width="160" prop="createTime"/>
      <el-table-column label="操作员" align="center" width="140" prop="operator"/>
      <el-table-column label="触发规则">
        <template slot-scope="{row}" >
          <span v-if="row.reason"> {{ row.reason }} </span>
          <span v-else :title="row.allRules">
            {{ row.firstRuleGroupName }} {{ row.ruleGroupNameList.length>1?"...":'' }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="会员ID" align="center" width="120" prop="memberId"/>
      <el-table-column label="会员昵称" align="center" width="150" prop="nickName" />
      <el-table-column label="身份证" align="center" width="200" prop="id_num" />
      <el-table-column label="手机号码" align="center" width="150" prop="tel"/>
      <el-table-column label="零钱余额($)" align="center" width="160" prop="smallMoney"/>
  
      <el-table-column :label="$t('status')" width="120" align="center">
        <template slot-scope="{row}">
          <state-change :row="row" @changeStatusSuccee="getMenberSearch" :pageName="pageName" :isShowWhite="false"/>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="120">
        <template slot-scope="scope">
          <el-button type="primary" plain size="mini" @click="goToDetail(scope.row.memberId)">会员详情</el-button>
        </template>
      </el-table-column>
    </el-table>
   
    <el-row style="text-align:right;">
      <Pagination
        v-show="total>0"
        :total="total"
        :page.sync="query.pageIndex"
        :limit.sync="query.pageSize"
        @pagination="changePagination" />
    </el-row>
  </div>
</template>
<script>
import {
  memberSearch,
} from '@/api/user-exception-filtering/abnormal-behavior';
import { changeStatusJS, watchRouterJs } from './../mixins/common.js';
import Pagination from '@/components/Pagination';
import StateChange from '@/views/member-exception-filtering/components/StateChange.vue';

export default {
  name: 'ExceptionUserManagement',
  components: {
    Pagination,
    StateChange
  },
  mixins: [changeStatusJS, watchRouterJs],
  data() {
    return {
      pageName: '会员异常筛选 / 会员异常管理',
      userTypeDic: {
        GREYANDBLACK: '全部',
        GREY: '灰名单',
        BLACK: '黑名单'
      },
      tableData: [],
      listLoading: false,
      total: 5,
      ruleOptions: [],
      searchDate: [],
      query: {
        memberId: '',
        nickName: '',
        tel: '',
        ruleNameFragment: '',
        beginTime: '',
        endTime: '',
        pageIndex: 1,
        pageSize: 50,
        type: 'GREYANDBLACK'
      },
      // 时间选择限制
      pickerMinDate: '',
      pickerOptions: {
        onPick: ({ maxDate, minDate }) => {
          this.pickerMinDate = minDate.getTime();
          if (maxDate) {
            this.pickerMinDate = '';
          }
        },
        disabledDate: time => {
          return time.getTime() > Date.now();
        }
      },
      show: {
        oneWhite: false
      },
      oneWhite: {},
      isCanBack: false
    };
  },

  activated() {
    if (!this.isCache) {
      this.query = {
        memberId: '',
        nickName: '',
        tel: '',
        ruleNameFragment: '',
        beginTime: '',
        endTime: '',
        pageIndex: 1,
        pageSize: 50,
        type: 'GREYANDBLACK'
      };
      this.query.memberId = this.$route.query.memberId == undefined ? '' : this.$route.query.memberId;
      this.isCanBack = !!this.$route.query.memberId;
      this.getMenberSearch();
    }
  },

  methods: {
    getMenberSearch() {
      this.listLoading = true;
      memberSearch(this.query)
        .then(result => {
          if (result.code === 200) {
            this.listLoading = false;
            this.tableData = result.data.map(item => {
              item.createTime =
                item.createTime === undefined
                  ? ''
                  : item.createTime.split('T').join(' ');
              return item;
            });
            this.tableData = this.formatRule(
              JSON.parse(JSON.stringify(this.tableData))
            );
            this.total = result.count;
          } else {
            this.listLoading = false;
            this.$message.error(result.msg);
          }
        })
        .catch(() => {
          this.listLoading = false;
        });
    },

    changeType(type) {
      this.query.type = type;
      this.getMenberSearch();
    },

    changePagination() {
      this.getMenberSearch();
    },

    search() {
      this.query.pageIndex = 1;
      this.getMenberSearch();
    }
  }
};
</script>
<style lang="scss" scoped>
.container {
  padding: 20px;
  height: calc(100vh-50px);
  overflow-y: auto;
}
.text-align--right {
  text-align: right;
}
.width {
  &-180 {
    width: 180px;
  }
}
.batchProcess {
  float: right;
  margin-top: 12px;
}
</style>
